import React, { Component } from 'react'
import axios from "axios"
// 防抖
import {debounce} from "lodash" // debounce就是防抖
class Login extends Component {
    state = {
        username:"",
        password:""
    }
    submit(){
        console.log(this.state.username);
        console.log(this.state.password);
        axios.get("/login1",{  params:{username:this.state.username,password:this.state.password}}).then(res=>{
            console.log(res);
            if (res.data.code == 1) {
                window.localStorage.setItem("islogin","true")
                setTimeout(()=>{
                    this.props.history.push("/my")
                },100)
            } else {
                window.localStorage.setItem("islogin","false")
            }
        })
    }
    changeUser(e){
        this.setState({
            username:e.target.value
        })
    }
    changePass(e){
        this.setState({
            password:e.target.value
        })
    }
  render() {
    return (
      <div>
          <div>
              <p><span>用户名</span><input type="text" placeholder='请输入用户名' value={this.state.username} onChange={this.changeUser.bind(this)}/></p>
              <p><span>密码</span><input type="text" placeholder='请输入入密码'  value={this.state.password} onChange={this.changePass.bind(this)}/></p>
              <button onClick={debounce(this.submit.bind(this),500)}>登陆</button>
          </div>
      </div>
    )
  }
}

export default Login